package com.teng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.teng.util.DBUtil;

;

public class DocDao {
	public void deleteDoc(String id)throws Exception{
		Connection connection=DBUtil.getConnection();
		int a=0;
		try {
			StringBuilder sql=new StringBuilder();
			sql.append(" select parent_id from doc_document_attachment where id=? and is_deleted=1");
			PreparedStatement ps=connection.prepareStatement(sql.toString());
			ps.setString(1,id);
			ResultSet rs=ps.executeQuery();
			while(rs.next()) {
				if(rs.getString(1)==null) {
					StringBuilder delsql=new StringBuilder();
					delsql.append("delete from doc_document_attachment where  is_deleted=1 and (id=? or parent_id=?)");
					ps=connection.prepareStatement(delsql.toString());
					ps.setString(1,id);
					ps.setString(2,id);
					ps.executeUpdate();
					connection.commit();
				}else {
					StringBuilder delsql1=new StringBuilder();
					delsql1.append("delete from doc_document_attachment where  is_deleted=1 and id=? ");
					ps=connection.prepareStatement(delsql1.toString());
					ps.setString(1,id);
					ps.executeUpdate();
					connection.commit();
				}
			}
		}finally {
			connection.close();
		}
	}
	
	public void reductionDoc(String id)throws Exception{
		Connection connection=DBUtil.getConnection();
		try {
			StringBuilder sql=new StringBuilder();
			sql.append(" update doc_document_attachment set is_deleted=0 where id=? ");
			PreparedStatement ps=connection.prepareStatement(sql.toString());
			ps.setString(1,id);
			ps.executeUpdate();
			connection.commit();
		}finally {
			connection.close();
		}
	}
	
	public String selectSavePath(String id)throws Exception{
		Connection connection=DBUtil.getConnection();
		String savePath=null;
		try {
			StringBuilder sql=new StringBuilder();
			sql.append("select save_path from doc_document_attachment where id=? and is_deleted=1");
			PreparedStatement ps=connection.prepareStatement(sql.toString());
			ps.setString(1,id);
			System.out.println(id);
			ResultSet rs=ps.executeQuery();
			if(rs.next()) {
				savePath=rs.getString("save_path");
			}
			return savePath;
		}finally {
			connection.close();
		}
		
	}
	
	public void emptyRecovery()throws Exception{
		Connection connection=DBUtil.getConnection();
		try {
			StringBuilder sql=new StringBuilder();
			sql.append("delete from doc_document_attachment");
			PreparedStatement ps=connection.prepareStatement(sql.toString());
			ps.executeUpdate();
			connection.commit();
		}finally {
			connection.close();
		}
	}
}
